package com.gkd.controller.admin;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.gkd.pojo.dto.PostDTO;
import com.gkd.pojo.dto.PostPageQueryDTO;
import com.gkd.pojo.entity.Post;
import com.gkd.pojo.vo.PostVO;
import com.gkd.result.PageResult;
import com.gkd.result.Result;
import com.gkd.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 文章相关模块
 */
@RestController
@CrossOrigin
@Slf4j
@RequestMapping("/admin/post")
public class PostController {

    @Autowired
    private PostService postService;

    /**
     * 文章上传
     * @param postDTO
     * @return
     */
    @PostMapping
    public Result add(@RequestBody PostDTO postDTO){
        log.info("文章上传:{}",postDTO);
        postService.add(postDTO);
        return Result.success();
    }


    /**
     * 文章分页查询
     * @param postPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(PostPageQueryDTO postPageQueryDTO){
        log.info("文章分页查询:{}",postPageQueryDTO);
        PageResult pageResult=postService.pageQuery(postPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 修改文章
     * @param postDTO
     */
    @PutMapping
    public Result update(@RequestBody PostDTO postDTO){
        log.info("文章修改:{}",postDTO);
        postService.update(postDTO);
        return Result.success();
    }

    /**
     * 根据文章id获取数据
     * @param postId
     * @return
     */
    @GetMapping("/{postId}")
    public Result<PostVO> getById(@PathVariable Integer postId){
        log.info("根据id获取文章:{}",postId);
        PostVO postVO=postService.getById(postId);
        return Result.success(postVO);
    }

    /**
     * 文章状态更改
     * @param status
     * @param postId
     * @return
     */
    @PutMapping("/status/{status}")
    public Result status(@PathVariable Integer status,Integer postId){
        log.info("文章状态更改:{}",status);
        Post post = Post.builder()
                .postId(postId)
                .status(status)
                .updateTime(new Date())
                .build();
        postService.updateById(post);
        return Result.success();
    }

    /**
     * 删除文章与关联标签
     * @param postIds
     */
    @DeleteMapping
    public Result deleteByIds(@RequestBody List<Integer> postIds){
        log.info("删除文章:{}",postIds);
        postService.deleteByIds(postIds);
        return Result.success();
    }


}
